//
//  BCProjectDetailCommonProblemsCell.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/22.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "BCProjectDetailCommonProblemsCell.h"

#import <OCSkeleton.h>

CGFloat BCProjectDetailCommonProblemsCellTopPadding = 3.0f;
CGFloat BCProjectDetailCommonProblemsCellBottomPadding = 10.0f;

CGFloat BCProjectDetailCommonProblemsCellContentPadding = 8.0f;
//
CGFloat BCProjectDetailCommonProblemsCellLatterWidth = 13.0f;
//
CGFloat BCProjectDetailCommonProblemsCellLabelPadding = 6.0f;

@interface BCProjectDetailCommonProblemsCell () <OCGradientsOwner>

@property (nonatomic, strong) UILabel *latterQuestionLabel;

@property (nonatomic, strong) UILabel *latterAnswerLabel;

@property (nonatomic, strong) UILabel *questionLabel;

@property (nonatomic, strong) UILabel *answerLabel;



@property (nonatomic, strong) OCGradientContainerView *qContainerView;
@property (nonatomic, strong) OCGradientContainerView *aContainerView;


@end

@implementation BCProjectDetailCommonProblemsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.fd_enforceFrameLayout = true;
        [self setupSubviews];
        [self setContainerViews];
    }
    return self;
}

- (void)setupSubviews {
    self.latterQuestionLabel = [UILabel new];
    self.latterQuestionLabel.textColor = [UIColor whiteColor];
    self.latterQuestionLabel.textAlignment = NSTextAlignmentCenter;
    self.latterQuestionLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
    self.latterQuestionLabel.backgroundColor = [UIColor darkMainColor];
    [self.contentView addSubview:self.latterQuestionLabel];
    
    self.latterAnswerLabel = [UILabel new];
    self.latterAnswerLabel.textAlignment = NSTextAlignmentCenter;
    self.latterAnswerLabel.textColor = [UIColor darkMainColor];
    self.latterAnswerLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
    [self.contentView addSubview:self.latterAnswerLabel];
    self.latterAnswerLabel.layer.borderColor = [UIColor darkMainColor].CGColor;
    self.latterAnswerLabel.layer.borderWidth = 0.5;
    //
    self.questionLabel = [UILabel new];
    self.questionLabel.numberOfLines = 0;
    self.questionLabel.textColor = [UIColor qmui_colorWithHexString:@"#333333"];
    self.questionLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    [self.contentView addSubview:self.questionLabel];
    //
    self.answerLabel = [UILabel new];
    self.answerLabel.numberOfLines = 0;
    self.answerLabel.textColor = [UIColor qmui_colorWithHexString:@"#AEAEAE"];
    self.answerLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    [self.contentView addSubview:self.answerLabel];
    
    //
    CGFloat lableLeftPadding = BCProjectDetailCellLeftPadding+BCProjectDetailCommonProblemsCellContentPadding+BCProjectDetailCommonProblemsCellLatterWidth;
    CGFloat labelMinHeight = self.questionLabel.font.lineHeight;
    [self.questionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(BCProjectDetailCommonProblemsCellTopPadding);
        make.left.mas_equalTo(self.contentView).offset(lableLeftPadding);
        make.right.mas_equalTo(self.contentView).offset(-BCProjectDetailCellRightPadding);
        make.height.mas_greaterThanOrEqualTo(labelMinHeight);
    }];
    //
    [self.answerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.questionLabel.mas_bottom).offset(BCProjectDetailCommonProblemsCellLabelPadding);
        make.left.right.mas_equalTo(self.questionLabel);
        make.height.mas_greaterThanOrEqualTo(labelMinHeight);
    }];
    
    //
    CGSize latterSize = CGSizeMake(BCProjectDetailCommonProblemsCellLatterWidth, BCProjectDetailCommonProblemsCellLatterWidth);
    [self.latterQuestionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(BCProjectDetailCellLeftPadding);
        make.size.mas_equalTo(latterSize);
        make.centerY.mas_equalTo(self.questionLabel.mas_top).offset(self.questionLabel.font.lineHeight/2);
    }];
    [self.latterAnswerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(BCProjectDetailCellLeftPadding);
        make.size.mas_equalTo(latterSize);
        make.centerY.mas_equalTo(self.answerLabel.mas_top).offset(self.answerLabel.font.lineHeight/2);
    }];
}
- (void)setContainerViews {
    self.qContainerView = [OCGradientContainerView new];
    [self.contentView addSubview:self.qContainerView];
    [self.qContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.latterQuestionLabel.mas_left);
        make.right.mas_equalTo(self.questionLabel.mas_right);
        make.height.mas_equalTo(self.questionLabel.font.lineHeight);
        make.top.mas_equalTo(self.questionLabel);
    }];
    //
    self.aContainerView = [OCGradientContainerView new];
    [self.contentView addSubview:self.aContainerView];
    [self.aContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.latterAnswerLabel.mas_left);
        make.right.mas_equalTo(self.answerLabel.mas_right);
        make.height.mas_equalTo(self.answerLabel.font.lineHeight);
        make.top.mas_equalTo(self.answerLabel);
    }];
}



- (CGSize)sizeThatFits:(CGSize)size {
    CGFloat height = 0;
    height += BCProjectDetailCommonProblemsCellTopPadding;
    if (self.problem) {
        CGFloat Width = size.width-BCProjectDetailCellLeftPadding-BCProjectDetailCommonProblemsCellContentPadding-BCProjectDetailCommonProblemsCellLatterWidth-BCProjectDetailCellRightPadding;
        CGSize qSize = [self.questionLabel sizeThatFits:CGSizeMake(Width, CGFLOAT_MAX)];
        CGSize aSize = [self.answerLabel sizeThatFits:CGSizeMake(Width, CGFLOAT_MAX)];
        //
        height += qSize.height;
        height += BCProjectDetailCommonProblemsCellLabelPadding;
        height += aSize.height;
    }else {
        height += self.questionLabel.font.lineHeight;
        height += BCProjectDetailCommonProblemsCellLabelPadding;
        height += self.answerLabel.font.lineHeight;
    }
    height += BCProjectDetailCommonProblemsCellBottomPadding;
    return CGSizeMake(size.width, height);
}

- (void)setProblem:(BCProjectDetailCommonProblemModel *)problem {
    _problem = problem;
    if (problem) {
        self.aContainerView.alpha = 0;
        self.qContainerView.alpha = 0;
        //
        self.latterAnswerLabel.alpha = 1;
        self.latterQuestionLabel.alpha = 1;
        self.questionLabel.text = problem.question;
        self.answerLabel.text = problem.answer;
        self.latterQuestionLabel.text = @"Q";
        self.latterAnswerLabel.text = @"A";
    }else {
        self.latterAnswerLabel.alpha = 0;
        self.latterQuestionLabel.alpha = 0;
        self.questionLabel.text = nil;
        self.answerLabel.text = nil;
        //
        self.aContainerView.alpha = 1;
        self.qContainerView.alpha = 1;
    }
}


#pragma mark - OCGradientsOwner
- (NSArray <OCGradientLayer *>*)gradientLayers {
    if (self.aContainerView && self.qContainerView) {
        return @[self.aContainerView.gradientLayer,self.qContainerView.gradientLayer];
    }
    return nil;
}

- (NSArray <UIView *>*)skeletonViews {
    if (self.qContainerView && self.aContainerView) {
        return @[self.qContainerView,self.aContainerView];
    }
    return nil;
}

@end
